package com.im.server.session;

import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Function:
 *
 * @author crossoverJie Date: 22/05/2018 18:33
 * @since JDK 1.8
 */
public class SessionSocketHolder {
  private static final Map<Long, NioSocketChannel> CHANNEL_MAP = new ConcurrentHashMap<>(16);

  public static void putChannel(Long id, NioSocketChannel socketChannel) {
    CHANNEL_MAP.put(id, socketChannel);
  }

  public static NioSocketChannel getChannel(Long id) {
    return CHANNEL_MAP.get(id);
  }

  public static Map<Long, NioSocketChannel> getChannelMap() {
    return CHANNEL_MAP;
  }

  public static void removeChannel(NioSocketChannel nioSocketChannel) {
    CHANNEL_MAP.entrySet().stream()
        .filter(entry -> entry.getValue() == nioSocketChannel)
        .forEach(entry -> CHANNEL_MAP.remove(entry.getKey()));
  }
}
